<?php

namespace App\Http\Models;

class Menu extends Base
{
    //获取tree
    public static function getMenuTree()
    {
        $menus = self::get()->toArray();
        return self::getTree($menus);
    }

    //获取tree，递归调用获取2层menu

    /**
     * @param $arr
     * @param int $pid
     * @return array
     */

    /**
     * protected static function getTree($arr, $pid = 0)
     * {
     * $tree = [];
     * foreach ($arr as $k => $v) {
     * if ($v['parent_id'] == $pid) {
     * $v['children'] = static::getTree($arr, $v['id']);
     * $tree[] = $v;
     * }
     * }
     * return $tree;
     * }
     */
    protected static function getTree($arr, $pid = 0)
    {
        $tree = [];
        foreach ($arr as $key => $v) {
            if ($v['parent_id'] == $pid) {
                $v['children'] = static::getTree($arr, $v['id']);
                $tree[] = $v;
            }
        }
        return $tree;
    }

}
